﻿using System;
using System.Drawing;
using System.Web.UI;
using Reviewer.Utilities.Drawing;
using Reviewer.Web.UI;

public partial class CMSV2_Controls_ImageUploader : System.Web.UI.UserControl, IImageUploaderControl
{
    public event ImageTransformationCompleteDelegate ImageTransformationComplete;

    public int MaxWidth
    {
        get
        {
            if (ViewState["MaxWidth"] == null)
                return 0;
            return (int)ViewState["MaxWidth"];
        }
        set { ViewState["MaxWidth"] = value; Reset(); }
    }

    public int MaxHeight
    {
        get
        {
            if (ViewState["MaxHeight"] == null)
                return 0;
            return (int)ViewState["MaxHeight"];
        }
        set { ViewState["MaxHeight"] = value; Reset(); }
    }

    public bool ExactSize
    {
        get
        {
            if (ViewState["ExactSize"] == null)
                return false;
            return (bool)ViewState["ExactSize"];
        }
        set
        {
            ViewState["ExactSize"] = value;
            rblCropMode.Items.FindByValue("1").Enabled = !value;
            Reset();
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Reset();
        }
    }

    void Reset()
    {
        litImageSizeHeightMax.Text = MaxHeight.ToString();
        litImageSizeWidthMax.Text = MaxWidth.ToString();

        litImageSizeHeightMaxExact.Visible = !ExactSize;
        litImageSizeWidthMaxExact.Visible = !ExactSize;

        litImageSizeIsExact.Visible = ExactSize;
        litImageSizeIsNotExact.Visible = !ExactSize;

        rblCropMode.Items.FindByValue("3").Enabled = !(MaxWidth == 0 || MaxHeight == 0);
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
        {
            return;
        }

        try
        {
            using (System.Drawing.Image bitmapImage = Bitmap.FromStream(fuFile.PostedFile.InputStream))
            {
                int imageWidth = bitmapImage.Size.Width;
                int imageHeight = bitmapImage.Size.Height;

                Bitmap tranformedImage = null;
                try
                {
                    switch (rblCropMode.SelectedValue)
                    {
                        case "0":
                            tranformedImage = ImageUtility.CropImage(bitmapImage, MaxWidth == 0 ? imageWidth : MaxWidth, MaxHeight == 0 ? imageHeight : MaxHeight, ImageAnchorPosition.Top) as Bitmap;
                            break;
                        case "1":
                            if (MaxHeight == 0)
                            {
                                tranformedImage = ImageUtility.ScaleImageWithProportions(bitmapImage, MaxWidth, ImageDimension.Width) as Bitmap;
                            }
                            else if (MaxWidth == 0)
                            {
                                tranformedImage = ImageUtility.ScaleImageWithProportions(bitmapImage, MaxHeight, ImageDimension.Height) as Bitmap;
                            }
                            else
                            {
                                tranformedImage = ImageUtility.ScaleImageWithProportions(bitmapImage, MaxWidth, MaxHeight) as Bitmap;
                            }

                            break;
                        case "2":
                            double heightRatio = int.MinValue;
                            if (MaxHeight > 0)
                            {
                                heightRatio = Convert.ToDouble(MaxHeight) / Convert.ToDouble(imageHeight);
                            }

                            double widthRatio = int.MinValue;
                            if (MaxWidth > 0)
                            {
                                widthRatio = Convert.ToDouble(MaxWidth) / Convert.ToDouble(imageWidth);
                            }

                            if (heightRatio < widthRatio || heightRatio == widthRatio)
                            {
                                tranformedImage = ImageUtility.ScaleImageWithProportions(bitmapImage, MaxWidth, ImageDimension.Width) as Bitmap;
                            }
                            else if (widthRatio < heightRatio)
                            {
                                tranformedImage = ImageUtility.ScaleImageWithProportions(bitmapImage, MaxHeight, ImageDimension.Height) as Bitmap;
                            }

                            tranformedImage = ImageUtility.CropImage(tranformedImage, MaxWidth == 0 ? tranformedImage.Width : MaxWidth, MaxHeight == 0 ? tranformedImage.Height : MaxHeight, ImageAnchorPosition.Center) as Bitmap;
                            break;
                        case "3":
                            tranformedImage = ImageUtility.ResizeImage(bitmapImage, MaxWidth, MaxHeight) as Bitmap;
                            tranformedImage.MakeTransparent(Color.Red);
                            break;

                    }

                    if (this.ImageTransformationComplete != null)
                    {
                        this.ImageTransformationComplete(this, tranformedImage);
                    }
                }
                finally
                {
                    if (tranformedImage != null)
                    {
                        tranformedImage.Dispose();
                        tranformedImage = null;
                    }
                }
            }
        }
        catch (Exception error)
        {
            System.Diagnostics.Debug.WriteLine(error.ToString());
            cvChangeTopImage.IsValid = false;
            throw;
        }
    }
}
